package dxn.nl.file.biz.manager;

import io.minio.*;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
@Component
public class MinIoImageManager {

    @Resource
    private MinioClient minioClient;

    public boolean existBucketName(String bucketName) throws Exception {
        BucketExistsArgs args = BucketExistsArgs.builder().bucket(bucketName).build();
        return minioClient.bucketExists(args);
    }

    public String saveFile(String bucketName, MultipartFile file, String filePath)
    {
        try{

            if(StringUtils.isBlank(bucketName))
            {
                return "存储桶名称不能为空";
            }

            BucketExistsArgs args = BucketExistsArgs.builder().bucket(bucketName).build();
            if(!minioClient.bucketExists(args))
            {
                MakeBucketArgs makeArgs = MakeBucketArgs.builder().bucket(bucketName).build();
                minioClient.makeBucket(makeArgs);
            }

            PutObjectArgs putObjectArgs = PutObjectArgs.builder().bucket(bucketName)
                    .object(filePath).stream(file.getInputStream(),file.getSize(),file.getSize()).build();
            ObjectWriteResponse response = minioClient.putObject(putObjectArgs);
            return null;
        }
        catch(Exception ex)
        {
            log.error("MinioImageManager",ex);
            return ex.getMessage();
        }
    }

    public String saveFile(String bucketName,String fileName, String filePath)
    {
        try{

            if(StringUtils.isBlank(bucketName))
            {
                return "存储桶名称不能为空";
            }

            if(StringUtils.isBlank(bucketName))
            {
                return "存储桶名称不能为空";
            }

            BucketExistsArgs args = BucketExistsArgs.builder().bucket(bucketName).build();
            if(!minioClient.bucketExists(args))
            {
                MakeBucketArgs makeArgs = MakeBucketArgs.builder().bucket(bucketName).build();
                minioClient.makeBucket(makeArgs);
            }

            UploadObjectArgs putObjectArgs = UploadObjectArgs.builder().bucket(bucketName)
                    .object(filePath).filename(fileName).build();

            minioClient.uploadObject(putObjectArgs);
            return null;
        }
        catch(Exception ex)
        {
            log.error("MinioImageManager",ex);
            return ex.getMessage();
        }
    }

    public InputStream getImage(String bucketName, String imagePath,long position,long length)
    {
        try{
            if(StringUtils.isBlank(bucketName))
            {
                throw new RuntimeException("存储桶名称不能为空");
            }

            return minioClient.getObject(
                    GetObjectArgs.builder().bucket(bucketName).object(imagePath).offset(position).length(length).build());
        }
        catch(Exception ex)
        {
            log.error("MinioImageManager",ex);
        }
        return null;
    }

    public InputStream getImage(String bucketName,String imagePath)
    {
        try{
            if(StringUtils.isBlank(bucketName))
            {
                throw new RuntimeException("存储桶名称不能为空");
            }

            return minioClient.getObject(
                    GetObjectArgs.builder().bucket(bucketName).object(imagePath).build());
        }
        catch(Exception ex)
        {
            log.error("MinioImageManager",ex);
        }
        return null;
    }

    public Boolean deleteImage(String bucketName,String imagePath)
    {
        try{
            if(StringUtils.isBlank(bucketName))
            {
                throw new RuntimeException("存储桶名称不能为空");
            }

            minioClient.removeObject(RemoveObjectArgs.builder()
                    .bucket(bucketName)
                    .object(imagePath)
                    .build());
            return  true;
        }
        catch(Exception ex)
        {
            log.error("MinioImageManager",ex);
        }
        return false;
    }
}
